package com.pan.admin.module.loanProduct.controller;

import com.pan.admin.common.annotation.Log;
import com.pan.admin.common.core.controller.BaseController;
import com.pan.admin.common.core.domain.AjaxResult;
import com.pan.admin.common.core.page.TableDataInfo;
import com.pan.admin.common.enums.BusinessType;
import com.pan.admin.common.utils.SecurityUtils;
import com.pan.admin.common.utils.poi.ExcelUtil;
import com.pan.admin.module.loanProduct.entity.LoanProduct;
import com.pan.admin.module.loanProduct.entity.vo.LoanProductBalanceOperationVO;
import com.pan.admin.module.loanProduct.service.LoanProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 产品Controller
 *
 * @author ts
 * @date 2023-09-12
 */
@RestController
@RequestMapping("/product/loanProduct")
public class LoanProductController extends BaseController {
    @Autowired
    private LoanProductService loanProductService;

    /**
     * 查询产品列表
     */
    @PreAuthorize("@ss.hasPermi('product:loanProduct:list')")
    @GetMapping("/list")
    public TableDataInfo list(LoanProduct loanProduct) {
        startPage();
        List<LoanProduct> list = loanProductService.listWithDataScope(loanProduct);
        return getDataTable(list);
    }

    /**
     * 查询所有产品
     */
    @PreAuthorize("@ss.hasPermi('product:loanProduct:list')")
    @GetMapping("/listAll")
    public AjaxResult listAll(LoanProduct loanProduct) {
        return success(loanProductService.listWithDataScope(loanProduct));
    }

    /**
     * 查重
     */
    @PreAuthorize("@ss.hasPermi('product:loanProduct:list')")
    @GetMapping("/check")
    public AjaxResult checkName(LoanProduct loanProduct) {
        boolean flag = loanProductService.checkName(loanProduct.getName(), loanProduct.getId());
        return AjaxResult.success(flag);
    }

    /**
     * 导出产品列表
     */
    @PreAuthorize("@ss.hasPermi('product:loanProduct:export')")
    @Log(title = "导出机构", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LoanProduct loanProduct) {
        List<LoanProduct> list = loanProductService.listWithDataScope(loanProduct);
        ExcelUtil<LoanProduct> util = new ExcelUtil<LoanProduct>(LoanProduct.class);
        util.exportExcel(response, list, "产品数据");
    }

    /**
     * 获取产品详细信息
     */
    @PreAuthorize("@ss.hasPermi('product:loanProduct:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        LoanProduct loanProduct = new LoanProduct();
        loanProduct.setId(id);
        return success(loanProductService.getByIdWithDataScope(loanProduct));
    }

    /**
     * 新增产品
     */
    @PreAuthorize("@ss.hasPermi('product:loanProduct:add')")
    @Log(title = "机构管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LoanProduct loanProduct) {

        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        loanProduct.setDataScopeUserId(userId);

        return toAjax(loanProductService.insertLoanProduct(loanProduct));
    }

    /**
     * 修改产品
     */
    @PreAuthorize("@ss.hasPermi('product:loanProduct:edit')")
    @Log(title = "机构管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LoanProduct loanProduct) {
        loanProductService.checkDataScope(loanProduct.getId());
        return toAjax(loanProductService.updateLoanProduct(loanProduct));
    }

    /**
     * 修改产品的状态
     */
    @PreAuthorize("@ss.hasPermi('product:loanProduct:edit')")
    @Log(title = "机构管理", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStatusOfLoanProduct")
    public AjaxResult updateStatusOfLoanProduct(@RequestBody LoanProduct loanProduct) {
        loanProductService.checkDataScope(loanProduct.getId());
        return toAjax(loanProductService.updateStatusOfLoanProduct(loanProduct));
    }

    /**
     * 充值或扣款
     */
    @PreAuthorize("@ss.hasPermi('product:loanProduct:amount')")
    @Log(title = "操作机构金额", businessType = BusinessType.UPDATE)
    @PostMapping("recharge")
    public AjaxResult balanceOperation(@RequestBody LoanProductBalanceOperationVO balanceOperationVO) {
        loanProductService.checkDataScope(balanceOperationVO.getId());
        loanProductService.balanceOperation(balanceOperationVO);
        return toAjax(true);
    }

}
